using System;
using System.Collections;
using System.Html;
using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;
using YesPlayLove.Clusters;
using Yoshi.Classes;
using YoshiBase.Classes;
using jQueryApi;

namespace YesPlayLove.Classes
{
    //
    // Page hiarchy: body > #PageMaster > #PageCenter > #PageContent||.<PageClassName>||Element > .PageCluster
    //

    [IgnoreNamespace]
    public abstract class YoshiPage : ProfileTruple, IPage
    {
        protected jQueryObject PageMaster;
        protected jQueryObject PageFooter;
        protected jQueryObject PageCluster;
        private jQueryObject _focusElement;
        protected TopBar TopBar;

        protected YoshiPage()
        {
            Title = "Yoshi Page";
            PageMaster = jQuery.Select("#" + Master.PageMasterId);
            Inform.Log("Creating page instance FullName={0}", GetType().FullName);
        }

        #region IPage Members

        public string Title
        {
            get { return Document.Title; }
            set { Document.Title = value; }
        }

        public virtual void UpdatePage(PageUpdate pu)
        {
            if (TopBar != null)
                TopBar.UpdatePage(pu);
        }

        public void FocusEvent(jQueryEvent e)
        {
            _focusElement = Script.IsValue(e) ? jQuery.FromElement(e.Target) : null;
        }

        public bool IsFocused(jQueryObject el)
        {
            return _focusElement != null && el[0] == _focusElement[0];
        }

        #endregion

        protected void PublicBuildAw(Await awp, string pageTitle)
        {
            jQueryObject centerBox = jQuery.FromHtml("<div id='PageCenter'/>")
                .AppendTo(PageMaster)
                ;

            Element = jQuery.FromHtml("<div id='PageContent'/>")
                .AppendTo(centerBox)
                .AddClass(GetType().Name)
                ;

            Title = pageTitle;

            new Await()
                .AddFn(BasicPage)
                .AddFn(BasicCluster)
                .AddFn(BasicFooter)
                .AddAw(RefreshAw)
                .AddDx(delegate { FileBlobBase.SvCnt--; })
                .Commit(awp);
        }

        protected void BasicBuildAw(Await awp, string pageTitle)
        {
            Inform.Debug("BasicBuildAw");
            jQueryObject centerBox = jQuery.FromHtml("<div id='PageCenter'/>")
                .AppendTo(PageMaster)
                ;

            Element = jQuery.FromHtml("<div id='PageContent'/>")
                .AppendTo(centerBox)
                .AddClass(GetType().Name)
                ;

            Title = pageTitle;

            new Await()
                .AddFn(CreateTopBar)
                .AddFn(BasicPage)
                .AddAw(Master.FbData.AuthenticateAw)
                .AddAw(ProfileData.LoadMyProfileAw)
                .HandleDl(Exceptions.NewUser, delegate{Master.GoPage(AllPages.NewUserPage);})
                .AddFn(ShowTopUser)
                .AddFn(BasicCluster)
                .AddFn(BasicFooter)
                .AddAw(RefreshAw)
                .AddDx(delegate { FileBlobBase.SvCnt--; })
                .Commit(awp);
        }

        public virtual void RefreshAw(Await awp)
        {
            awp.Done();
        }

        protected void BasicPage()
        {
            jQueryObject topic = jQuery.FromHtml("<h1 id='Topic'/>")
                .Html(ThemeBase.SiteTitle)
                .AppendTo(Element)
                ;

            Actor yoshi = (Actor) new Actor().FromHtml("<img id='TopicImage'/>");

            yoshi.Element
                .AppendTo(topic)
                .Attribute("src", MagneticListPageTheme.LogoImage)
                .MouseEnter(delegate { yoshi.Shake(); })
                ;
        }

        protected void BasicCluster()
        {
            jQuery.FromHtml("<div id='PageGigs' class='FrontContent'/>")
                .AppendTo(Element)
                ;
            PageCluster = jQuery.FromHtml("<div class='PageCluster'/>")
                .AppendTo(Element)
                ;
        }

        protected void BasicFooter()
        {
            PageFooter = jQuery.FromHtml("<div class='PageFooter'/>")
                .AppendTo(Element)
                .Html("")
                ;
        }

        public virtual void Destroy()
        {
        }

        protected virtual void CreateTopBar()
        {
            TopBar = new TopBar(Element, new Dictionary("links", true));
        }

        public void ShowTopUser()
        {
            if (TopBar != null)
                TopBar.ShowUser();
        }

        public void LoadPageCssAw(Await awp)
        {
            new Await()
                .LoadCss("Scripts/Css/" + GetType().Name + ".css")
                .Commit(awp);
        }

        public void LoadNamespaceCssAw(Await awp)
        {
            new Await()
                .LoadCss("Scripts/Css/" + GetType().FullName.Split(".")[0] + ".css")
                .Commit(awp);
        }
    }
}